package models

import (
	"time"

	"gorm.io/gorm"
)

// StorageType 存储类型枚举
type StorageType string

const (
	StorageLocal   StorageType = "local"
	StorageMinIO   StorageType = "minio"
	StorageAliYun  StorageType = "aliyun"
	StorageTencent StorageType = "tencent"
)

// FileStorage 文件存储表模型
type FileStorage struct {
	ID          int64          `gorm:"primaryKey;autoIncrement" json:"id"`
	FileName    string         `gorm:"size:255;not null" json:"file_name"`
	FileKey     string         `gorm:"size:255;not null;uniqueIndex:uk_file_key" json:"file_key"`
	FileSize    int64          `gorm:"not null" json:"file_size"`
	FileType    string         `gorm:"size:100" json:"file_type"`
	FileExt     string         `gorm:"size:20" json:"file_ext"`
	StorageType StorageType    `gorm:"type:enum('LOCAL','MINIO','ALIYUN','TENCENT');not null" json:"storage_type"`
	BucketName  string         `gorm:"size:100" json:"bucket_name"`
	StoragePath string         `gorm:"size:500" json:"storage_path"`
	AccessURL   string         `gorm:"size:500" json:"access_url"`
	MD5         string         `gorm:"size:32;index:idx_md5" json:"md5"`
	SHA1        string         `gorm:"size:40" json:"sha1"`
	Status      int8           `gorm:"default:1" json:"status"`
	CreatedBy   int64          `gorm:"size:10;index:idx_created_by" json:"created_by"`
	CreatedAt   time.Time      `gorm:"not null" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"not null" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"-"`
}

func (f *FileStorage) TableName() string {
	return "sys_file_storage"
}

// StorageConfig 存储配置表模型
type StorageConfig struct {
	ID          int64       `gorm:"primaryKey;autoIncrement" json:"id"`
	Type        int         `gorm:"size:4;default:1" json:"type"`
	StorageType StorageType `gorm:"type:enum('LOCAL','MINIO','ALIYUN','TENCENT');not null" json:"code"`
	ConfigName  string      `gorm:"size:100;not null;uniqueIndex:uk_storage_config" json:"name"`
	Endpoint    string      `gorm:"size:255" json:"endpoint"`
	AccessKey   string      `gorm:"size:255" json:"accessKey"`
	SecretKey   string      `gorm:"size:255" json:"secretKey"`
	Region      string      `gorm:"size:100" json:"region"`
	BucketName  string      `gorm:"size:100" json:"bucketName"`
	Domain      string      `gorm:"size:255" json:"domain"`
	IsDefault   bool        `gorm:"default:false" json:"isDefault"`
	Status      int8        `gorm:"default:1" json:"status"`
	Sort        int         `gorm:"default:1" json:"sort"`
	Description string      `gorm:"size:255" json:"description"`
	CreatedAt   time.Time   `gorm:"not null" json:"createTime"`
	UpdatedAt   time.Time   `gorm:"not null" json:"updated_at"`
}

func (f *StorageConfig) TableName() string {
	return "sys_storage_config"
}

// FileMetadata 文件元数据表模型
type FileMetadata struct {
	ID        int64     `gorm:"primaryKey;autoIncrement" json:"id"`
	FileID    int64     `gorm:"not null;uniqueIndex:uk_file_meta" json:"file_id"`
	MetaKey   string    `gorm:"size:100;not null;uniqueIndex:uk_file_meta" json:"meta_key"`
	MetaValue string    `gorm:"type:text" json:"meta_value"`
	CreatedAt time.Time `gorm:"not null" json:"created_at"`
	UpdatedAt time.Time `gorm:"not null" json:"updated_at"`
}

func (f *FileMetadata) TableName() string {
	return "sys_file_metadata"
}
